<template>
  <a-layout-header class="header">
    <a-row :wrap="false">
      <!-- 左侧：Logo和标题 -->
      <a-col flex="200px">
        <RouterLink to="/">
          <div class="header-left">
            <img class="logo" src="@/assets/logo.png" alt="Logo" />
            <h1 class="site-title">ZeroAI</h1>
          </div>
        </RouterLink>
      </a-col>
      <!-- 中间：导航菜单 -->
      <a-col flex="auto">
        <a-menu
          v-model:selectedKeys="selectedKeys"
          mode="horizontal"
          :items="menuItems"
          @click="handleMenuClick"
        />
      </a-col>
      <!-- 右侧：用户操作区域 -->
      <a-col>
        <div class="user-login-status">
          <div v-if="loginUserStore.loginUser.id">
            <a-dropdown>
              <a-space>
                <a-avatar :src="avatarSrc" />
                {{ loginUserStore.loginUser.userName ?? "无名" }}
              </a-space>
              <template #overlay>
                <a-menu>
                  <a-menu-item @click="doLogout">
                    <LogoutOutlined />
                    退出登录
                  </a-menu-item>
                </a-menu>
              </template>
            </a-dropdown>
          </div>
          <div v-else>
            <a-button type="primary" href="/user/login">登录</a-button>
          </div>
        </div>
      </a-col>
    </a-row>
  </a-layout-header>
</template>

<script setup lang="ts">
import { computed, h, ref } from "vue"
import { useRouter } from "vue-router"
import { useLoginUserStore } from "@/stores/loginUser.ts"
import { userLogout } from "@/api/userController.ts"
import { LogoutOutlined, HomeOutlined } from "@ant-design/icons-vue"
import defaultAvatar from "@/assets/logo.png"
import { type ItemType, type MenuProps, message } from "ant-design-vue"
import checkAccess from "@/access/checkAccess"

const router = useRouter()
const loginUserStore = useLoginUserStore()

// 当前选中菜单
const selectedKeys = ref<string[]>(["/"])
// 监听路由变化，更新当前选中菜单
router.afterEach((to) => {
  selectedKeys.value = [to.path]
})

// 菜单配置项
const originItems = [
  {
    key: "/",
    icon: () => h(HomeOutlined),
    label: "主页",
    title: "主页"
  },
  {
    key: "/admin/userManage",
    label: "用户管理",
    title: "用户管理"
  },
  {
    key: "/admin/appManage",
    label: "应用管理",
    title: "应用管理"
  }
]

// 过滤菜单项
const filterMenus = (menus = [] as MenuProps["items"]) => {
  return menus?.filter((menu) => {
    const item = menuToRouteItem(menu)
    return checkAccess(loginUserStore.loginUser, item?.meta?.access as string)
  })
}

// 菜单映射到路由
const menuToRouteItem = (menu: ItemType) => {
  const routes = router.getRoutes()
  return routes.find((route) => route.path === menu?.key)
}

// 展示在菜单的路由数组
const menuItems = computed<MenuProps["items"]>(() => filterMenus(originItems))

// 处理菜单点击
const handleMenuClick: MenuProps["onClick"] = (e) => {
  const key = e.key as string
  selectedKeys.value = [key]
  // 跳转到对应页面
  if (key.startsWith("/")) {
    router.push(key)
  }
}

const avatarSrc = computed(() => {
  return loginUserStore.loginUser.userAvatar || defaultAvatar
})

// 退出登录
const doLogout = async () => {
  const res = await userLogout()
  if (res.data.code === 0) {
    loginUserStore.setLoginUser({
      userName: "未登录"
    })
    message.success("退出登录成功")
    await router.push("/user/login")
  } else {
    message.error("退出登录失败，" + res.data.message)
  }
}
</script>

<style scoped>
.header {
  background: #fff;
  padding: 0 24px;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 12px;
}

.logo {
  height: 48px;
  width: 48px;
}

.avatar {
  height: 32px;
  width: 32px;
}

.site-title {
  margin: 0;
  font-size: 18px;
  color: #1890ff;
}

.ant-menu-horizontal {
  border-bottom: none !important;
}
</style>
